package com.slipper.ai.flow.support;

import com.slipper.ai.flow.INodeDealer;
import com.slipper.ai.flow.Parser;
import com.slipper.ai.flow.vo.*;
import com.slipper.constants.Constants;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component("aiEndNodeDealer")
public class EndNodeDealer implements INodeDealer {
    @Override
    public String getType() {
        return NodeVO.TYPE_END;
    }

    @Override
    public List<LineVO> doDeal(FlowVO flow, NodeVO node, NodeInstVO nodeInst) {
        nodeInst.setOutput(nodeInst.getInput());

        if (node instanceof EndNodeVO endNode && Constants.YES.equals(endNode.getIfText())) {
            nodeInst.setContent(Parser.parseSymbol(endNode.getContent(), nodeInst.getInput()));
        }

        return flow.getLines()
                .stream()
                .filter(line -> line.getSource().equals(node.getId()))
                .collect(Collectors.toList());
    }
}
